/*
 * Albareth - an action roleplaying game.
 * Copyright (C) 2008 Jens Anuth
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 *   02110-1301, USA.
 * 
 * $Id: TargetFrame.java 38 2008-05-07 20:00:46Z jens464 $
*/
package albareth.frontend.slick.ui;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;

import albareth.core.entity.Mobile;
import albareth.frontend.slick.SlickFrontend;
import albareth.frontend.slick.SlickResources;

public class TargetFrame extends UIElement
{
    private Mobile _target = null;
    //private Font _font = SlickResources.GAME_FONT;
    //private int _lineHeight;
    private Object[] _targetNameBroken = null;
    private float _hpPct = 0;
    private int _namePartWidth = 100;
    private float _tileScale = 2.0f;

    
    public TargetFrame()
    {
    }
    
    public void setTarget(Mobile target)
    {
        _target = target;
        if (_target == null) {
            return;
        }
        _targetNameBroken = UIToolkit.breakLines(
                _target.getName(),
                _namePartWidth,
                getFont()
        );
    }

    public void render(Graphics g, GameContainer container, int screenX, int screenY)
            throws SlickException
    {
        if (_target == null) {
            return;
        }
        
        //draw icon:
        SlickFrontend.drawTile(
                _target.getTile(), screenX, screenY + 3,
                _tileScale, null, false
        );

        //draw name
        g.setColor(Color.white);
        //g.setFont(_font);
        if (_targetNameBroken.length > 1)
        {
            for (int i = 0; i < _targetNameBroken.length; i++)
            {
                g.drawString(
                        ((String)_targetNameBroken[i]).toUpperCase(),
                        screenX + 34,
                        screenY + 3 + i * getFont().getLineHeight()
                );
            }
        } else {
            g.drawString(
                    ((String)_targetNameBroken[0]).toUpperCase(),
                    screenX + 34,
                    screenY + 12
            );

        }
        
        //draw hp-bar:
        UIToolkit.drawBar(
                screenX + 208, screenY + 11,
                SlickResources.HP_BAR_FULL, SlickResources.HP_BAR_EMPTY,
                _hpPct
        );



    }

    public boolean update(int delta, GameContainer container)
    {
        if (_target != null) {
            _hpPct = (float)_target.getHitpoints() / _target.getMaxHitpoints();
        }
        return false;
    }

}
